package com.snopy.designPattern.create.singleton;

/**
 * @author snopy
 * @version 1.0.0
 * @classname SingletonC
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/27 20:32
 */
public final class SingletonC {

    private SingletonC(){}

    private volatile static SingletonC INSTANCE;
    //    分析这里的线程安全性，并说明有什么缺点
    /*
    * 静态方法上加synchronized相当于SingletonC.class类对象加锁，没有线程安全问题
    * 缺点：每次线程获取单例对象，都会去竞争锁，效率低
    * */
    public static synchronized SingletonC getInstance(){
        if (INSTANCE!=null){
            return INSTANCE;
        }
        INSTANCE = new SingletonC();
        return INSTANCE;
    }
}
